<!--
 * @Author: lide1202@hotmail.com
 * @Date: 2021-3-13 11:04:24
 * @Last Modified by:   lide1202@hotmail.com
 * @Last Modified time: 2021-3-13 11:04:24
 !-->
<template>
  <div class="text" @click="hanldClick" :style="styleColor">{{ styleColor.text }}</div>
</template>

<script>
import { queryEchartsData } from "@/api/bigscreen";
export default {
  name: "WidgetText",
  components: {},
  props: {
    value: Object,
    ispreview: Boolean
  },
  data() {
    return {
      options: {},
      optionsData: {}
    };
  },
  computed: {
    transStyle() {
      return this.objToOne(this.options);
    },
    styleColor() {
      return {
        position: this.ispreview ? "absolute" : "static",
        color: this.transStyle.color,
        "font-weight": this.transStyle.fontWeight,
        text: this.transStyle.text,
        "font-size": this.transStyle.fontSize + "px",
        "letter-spacing": this.transStyle.letterSpacing + "em",
        background: this.transStyle.background,
        width: this.transStyle.width + "px",
        height: this.transStyle.height + "px",
        left: this.transStyle.left + "px",
        top: this.transStyle.top + "px",
        right: this.transStyle.right + "px",
        "border-radius": this.transStyle.borderRadius + 'px',
        "display": "flex",
        "align-items": this.transStyle.alignItem,
        "justify-content": this.transStyle.textAlign,
        "cursor": this.transStyle.canClick ? 'pointer': 'default'
      };
    }
  },
  watch: {
    value: {
      handler(val) {
        this.options = val;
        this.optionsData = val.data;
        this.setOptionsData();
      },
      deep: true
    }
  },
  mounted() {
    this.options = this.value;
    this.optionsData = this.value.data;
    this.setOptionsData();
  },
  methods: {
    // 点击事件
    hanldClick() {
      if(this.transStyle.canClick && this.transStyle.routerUrl) {
        const routerUrl = this.transStyle.routerUrl
        const jumpMode = this.transStyle.jumpMode
        if(routerUrl.startsWith('http://') || routerUrl.startsWith('https://')) {
          if(jumpMode === 'self'){
            window.location.href = routerUrl
          } else {
            window.open(routerUrl)
          }
        } else {
          if(jumpMode === 'self'){
            this.$router.push(routerUrl)
          } else {
            var routeUrl = this.$router.resolve({
              path: routerUrl,
            });
            window.open(routeUrl.href, "_blank");
          }

        }
      }
    },
    // 数据解析
    setOptionsData() {
      const optionsData = this.optionsData; // 数据类型 静态 or 动态
      if (optionsData.dataType == "dynamicData") {
        this.dynamicDataFn(optionsData.dynamicData, optionsData.refreshTime);
      } else {};
    },
    // 动态数据
    dynamicDataFn(val, refreshTime, optionsSetup) {
      // 如果没有请求参数不调接口
      let isQuery = val && val.url && val.dataMethod
      if (!isQuery) return;
      // 动态数据调接口
      if (this.ispreview) {
        this.getEchartData(val, optionsSetup);
        // 刷新时间为0不轮询接口
        if(refreshTime === 0) return
        this.flagInter = setInterval(() => {
          this.getEchartData(val, optionsSetup);
        }, refreshTime);
      } else {
        this.getEchartData(val, optionsSetup);
      }
    },
    getEchartData(val) {
      queryEchartsData(val)
        .then(res => {
          if (res.tx_status !== "00") return;
          this.options.setup.text = res.text;
        })
        .catch((err) => {
          this.$message.error(err);
        });
    },
  }
};
</script>

<style scoped lang="less">
.text {
  width: 100%;
  height: 100%;
  overflow: hidden;
}
</style>
